package com.yaozheng.vocationaltraining.view.question;

import net.sf.json.JSONArray;

import org.androidannotations.annotations.CheckedChange;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.ViewById;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.utils.TypeUtils;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.RadioButton;

@EViewGroup(R.layout.view_question_single_choice)
public class SingleChoiceQuestionView extends BaseQuestionView {
    @ViewById
    RadioButton optionsRadio1, optionsRadio2, optionsRadio3, optionsRadio4;
    @ViewById
    View optionsImage1, optionsImage2, optionsImage3, optionsImage4;


    public SingleChoiceQuestionView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public SingleChoiceQuestionView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void setOptionsContent(String... contents) {
        optionsRadio1.setText(contents[0]);
        optionsRadio2.setText(contents[1]);
        optionsRadio3.setText(contents[2]);
        optionsRadio4.setText(contents[3]);
    }

    @Override
    public void setOptionsContent(JSONArray contents) {
        optionsRadio1.setText(TypeUtils.getJsonString(contents, 0));
        optionsRadio2.setText(TypeUtils.getJsonString(contents, 1));
        optionsRadio3.setText(TypeUtils.getJsonString(contents, 2));
        optionsRadio4.setText(TypeUtils.getJsonString(contents, 3));

    }


    @Override
    public String getAnswer() {
        if (optionsRadio1.isChecked()) {
            return "A";
        } else if (optionsRadio2.isChecked()) {
            return "B";
        } else if (optionsRadio3.isChecked()) {
            return "C";
        } else if (optionsRadio4.isChecked()) {
            return "D";
        }
        return null;
    }



    @CheckedChange({R.id.optionsRadio1, R.id.optionsRadio2, R.id.optionsRadio3, R.id.optionsRadio4})
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            if (buttonView == optionsRadio1) {
                if (optionsImage1.getVisibility() == View.VISIBLE) {
                    optionsRadio1.setChecked(false);
                    return;
                }
                optionsRadio2.setChecked(false);
                optionsRadio3.setChecked(false);
                optionsRadio4.setChecked(false);

            } else if (buttonView == optionsRadio2) {
                if (optionsImage2.getVisibility() == View.VISIBLE) {
                    optionsRadio2.setChecked(false);
                    return;
                }
                optionsRadio1.setChecked(false);
                optionsRadio3.setChecked(false);
                optionsRadio4.setChecked(false);
            } else if (buttonView == optionsRadio3) {
                if (optionsImage3.getVisibility() == View.VISIBLE) {
                    optionsRadio3.setChecked(false);
                    return;
                }
                optionsRadio1.setChecked(false);
                optionsRadio2.setChecked(false);
                optionsRadio4.setChecked(false);
            } else if (buttonView == optionsRadio4) {
                if (optionsImage4.getVisibility() == View.VISIBLE) {
                    optionsRadio4.setChecked(false);
                    return;
                }
                optionsRadio1.setChecked(false);
                optionsRadio3.setChecked(false);
                optionsRadio3.setChecked(false);
            }

            if (onAnswerChange != null) {
                onAnswerChange.answerChange(this);
            }
        }
    }

    @Override
    public void setAnswer(String answer) {
        if (answer != null) {
            if ("A".equals(answer)) {
                optionsRadio1.setChecked(true);
            } else if ("B".equals(answer)) {
                optionsRadio2.setChecked(true);

            } else if ("C".equals(answer)) {
                optionsRadio3.setChecked(true);

            } else if ("D".equals(answer)) {
                optionsRadio4.setChecked(true);

            }
        }
    }

    @Override
    public void optionsCard(JSONArray optionsCard) {
        optionsImage1.setVisibility(View.GONE);
        optionsImage2.setVisibility(View.GONE);
        optionsImage3.setVisibility(View.GONE);
        optionsImage4.setVisibility(View.GONE);
        if (optionsCard != null) {
            for (int i = 0; i < TypeUtils.getJsonArraySize(optionsCard); i++) {
                String optionCard = TypeUtils.getJsonString(optionsCard, i);
                if ("A".equals(optionCard)) {
                    optionsImage1.setVisibility(View.VISIBLE);
                    optionsRadio1.setChecked(false);

                } else if ("B".equals(optionCard)) {
                    optionsImage2.setVisibility(View.VISIBLE);
                    optionsRadio2.setChecked(false);

                } else if ("C".equals(optionCard)) {
                    optionsImage3.setVisibility(View.VISIBLE);
                    optionsRadio3.setChecked(false);

                } else if ("D".equals(optionCard)) {
                    optionsImage4.setVisibility(View.VISIBLE);
                    optionsRadio4.setChecked(false);

                }
            }
        }
    }

}
